<template>
  <div class="treatment-process-chart">
    <div v-loading="loading" class="chart-container">
      <div ref="chartRef" class="chart"></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted, watch, nextTick } from 'vue'
import * as echarts from 'echarts'

interface ProcessDataPoint {
  time: number
  intensity: number
  frequency: number
}

interface Props {
  data: ProcessDataPoint[]
  loading?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  data: () => [],
  loading: false
})

const chartRef = ref<HTMLElement>()
let chartInstance: echarts.ECharts | null = null

const initChart = () => {
  if (!chartRef.value) return
  
  chartInstance = echarts.init(chartRef.value)
  updateChart()
}

const updateChart = () => {
  if (!chartInstance || !props.data?.length) return
  
  const times = props.data.map(item => item.time)
  const intensities = props.data.map(item => item.intensity)
  const frequencies = props.data.map(item => item.frequency)
  
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    legend: {
      data: ['电流强度', '频率'],
      top: 10
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: [
      {
        type: 'category',
        data: times,
        axisPointer: {
          type: 'shadow'
        },
        axisLabel: {
          formatter: (value: number) => {
            // 格式化时间为分钟:秒
            const minutes = Math.floor(value / 60)
            const seconds = value % 60
            return `${minutes}:${seconds.toString().padStart(2, '0')}`
          }
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        name: '电流强度 (mA)',
        min: 0,
        axisLabel: {
          formatter: '{value} mA'
        }
      },
      {
        type: 'value',
        name: '频率 (Hz)',
        min: 0,
        axisLabel: {
          formatter: '{value} Hz'
        }
      }
    ],
    series: [
      {
        name: '电流强度',
        type: 'line',
        yAxisIndex: 0,
        data: intensities,
        lineStyle: {
          color: '#409EFF',
          width: 3
        },
        itemStyle: {
          color: '#409EFF'
        },
        symbol: 'circle',
        symbolSize: 6,
        smooth: true
      },
      {
        name: '频率',
        type: 'line',
        yAxisIndex: 1,
        data: frequencies,
        lineStyle: {
          color: '#67C23A',
          width: 3
        },
        itemStyle: {
          color: '#67C23A'
        },
        symbol: 'circle',
        symbolSize: 6,
        smooth: true
      }
    ],
    animation: true,
    animationDuration: 1000,
    animationEasing: 'cubicOut'
  } as any
  
  chartInstance.setOption(option, true)
}

const resizeChart = () => {
  if (chartInstance) {
    chartInstance.resize()
  }
}

// 监听数据变化
watch(() => props.data, () => {
  nextTick(() => {
    updateChart()
  })
}, { deep: true })

// 监听加载状态
watch(() => props.loading, (loading) => {
  if (!loading) {
    nextTick(() => {
      updateChart()
    })
  }
})

onMounted(() => {
  nextTick(() => {
    initChart()
  })
  
  window.addEventListener('resize', resizeChart)
})

onUnmounted(() => {
  if (chartInstance) {
    chartInstance.dispose()
  }
  window.removeEventListener('resize', resizeChart)
})
</script>

<style scoped lang="scss">
.treatment-process-chart {
  .chart-container {
    position: relative;
    
    .chart {
      width: 100%;
      height: 400px;
    }
  }
}
</style>